﻿//===============================================================================
// Microsoft Platform Architecture Team
// LitwareHR - S+S Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace GeoRss
{
    public class Point
    {
        private readonly double longitude;
        private readonly double latitude;

        public double Longitude
        {
            get { return longitude; }
        }

        public double Latitude
        {
            get { return latitude; }
        }

        public Point(double latitude, double longitude)
        {
            this.latitude = latitude;
            this.longitude = longitude;
        }

        public static Point Parse(string coordinates)
        { 
            string[] c = coordinates.Split(' ');
            
            if (c.Length != 2)
            {
                throw new ArgumentException("Insufficient coordinates. There must be at least 2 numbers");
            }

            Double latitude;
            Double longitude;

            if (!Double.TryParse(c[0], NumberStyles.Any, CultureInfo.InvariantCulture, out latitude) || !Double.TryParse(c[1], NumberStyles.Any, CultureInfo.InvariantCulture, out longitude))
            {
                throw new ArgumentException("Coordinates could not be parsed into double numbers");    
            }

            return new Point(latitude, longitude);
        }

        public override string ToString()
        {
            return latitude.ToString(CultureInfo.InvariantCulture) + " " + longitude.ToString(CultureInfo.InvariantCulture);
        }
    }
}
